package controller

import (
	"com.haojuetrace/actualCombat/common"
	"com.haojuetrace/actualCombat/dto"
	"com.haojuetrace/actualCombat/model"
	"com.haojuetrace/actualCombat/response"
	"com.haojuetrace/actualCombat/util"
	"fmt"
	"github.com/gin-gonic/gin"
	"golang.org/x/crypto/bcrypt"
	"log"
	"net/http"
)

/*
用户的注册，登录，查看方法
*/

//Register 用户注册
func Register(c *gin.Context) {
	db := common.GetDb()
	u := model.Users{}
	//获取参数
	c.ShouldBind(&u)
	name := u.Name
	phoneNum := u.PhoneNum
	//fmt.Println(phoneNum)
	password := u.Password
	//数据验证

	//判断名称是否为空
	if len(name) == 0 {
		name = util.RandName(10)
	} else {
		if len(name) > 10 {
			response.ResPonse(c, http.StatusUnprocessableEntity, 422, nil, "名称不符合要求")
			return
		}
	}
	//判断手机号是否是11位
	if len(phoneNum) != 11 {
		response.ResPonse(c, http.StatusUnprocessableEntity, 422, nil, "请输入11位手机号码！")
		return
	}
	//判断密码是否大于6位
	if len(password) < 6 {
		response.ResPonse(c, http.StatusUnprocessableEntity, 422, nil, "密码不匹配，请输入相应格式！")
		return
	}
	//判断手机号是否存在
	exist, _ := common.UserExist("phone", phoneNum, db)
	if exist {
		response.ResPonse(c, http.StatusUnprocessableEntity, 422, nil, "用户已存在！")
		return
	}
	//创建用户
	passwords, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) //加密密码
	if err != nil {
		response.ResPonse(c, http.StatusInternalServerError, 500, nil, "加密错误！")
		return
	}
	user := model.Users{Name: name, PhoneNum: phoneNum, Password: string(passwords)}
	id, err := common.CreateUser(&user, db)
	if err != nil {
		fmt.Println(err)
		response.ResPonse(c, http.StatusUnprocessableEntity, 422, nil, "系统繁忙！")
		return
	}
	u.Id = int(id)
	//发放token
	token, err := common.RegisterToken(&u)
	if err != nil {
		log.Println("token generate error", err)
		response.ResPonse(c, http.StatusInternalServerError, 500, nil, "系统异常!")
		return
	}
	//返回结果
	response.Success(c, gin.H{"token": token}, "注册成功")

}

//Login 用户登录
func Login(c *gin.Context) {
	db := common.GetDb()
	newUser := model.Users{}
	//获取参数

	c.ShouldBind(&newUser)
	//校验数据
	//判断手机号是否是11位
	if len(newUser.PhoneNum) != 11 {
		response.ResPonse(c, http.StatusUnprocessableEntity, 422, nil, "请输入11位手机号码！")
		return
	}
	//判断密码是否大于6位
	if len(newUser.Password) < 6 {
		response.ResPonse(c, http.StatusUnprocessableEntity, 422, nil, "密码不匹配，请输入相应格式！")
		return
	}
	//判断手机号是否存在并判断密码是否正确
	exist, users := common.UserExist("phone", newUser.PhoneNum, db)
	if exist {
		err := bcrypt.CompareHashAndPassword([]byte(users.Password), []byte(newUser.Password))
		if err != nil {
			response.ResPonse(c, http.StatusUnprocessableEntity, 422, nil, "密码错误!")
			return
		}

	} else {
		response.ResPonse(c, http.StatusUnprocessableEntity, 422, nil, "用户不存在!")
		return
	}
	//发放token
	token, err := common.RegisterToken(users)
	if err != nil {
		log.Println("token generate error", err)
		response.ResPonse(c, http.StatusInternalServerError, 500, nil, "系统异常!")
		return
	}
	//返回结果
	response.Success(c, gin.H{"token": token}, "登录成功")
}

//Info 用户信息查看
func Info(c *gin.Context) {
	user, _ := c.Get("user") //获取中间件AuthMiddleware解析的user
	response.Success(c, gin.H{"user": dto.ToUserDto(user.(*model.Users))}, "")
}
